package com.usian.article.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.ArticleControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/author")
public class ArticleController implements ArticleControllerApi {

    @Autowired
    private ApAuthorService apAuthorService;


    @GetMapping("/findByUserId/{id}")
    @Override
    public ApAuthor findByUserId(@PathVariable Integer id) {
        List<ApAuthor> authorList = apAuthorService.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id));
        if(authorList!=null && authorList.size()>0){
            return authorList.get(0);
        }
        return null;
    }

    @PostMapping("/save")
    @Override
    public ResponseResult saveApAuthor(@RequestBody ApAuthor apAuthor) {
        boolean result = apAuthorService.save(apAuthor);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 文章审核 根据自媒体用户名称查询作者详情
     * @param name
     * @return
     */
    @Override
    @GetMapping("/findByName/{name}")
    public ApAuthor findByName(@PathVariable String name) {
        ApAuthor apAuthor = apAuthorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        return apAuthor;
    }

    /**
     * 用户关注 根据id查询作者信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ApAuthor findById(@PathVariable Integer id) {
        return apAuthorService.getById(id);
    }
}
